package com.unionfetch.content_home.adapter

import android.content.Context
import android.widget.ImageView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.unionfetch.content_home.R
import com.unionfetch.content_home.bean.UploadItem
import com.unionftech.baselibrary.glide.loadRoundImage
import com.unionftech.common.R.*

class UploadItemAdapter(context: Context, list: MutableList<UploadItem>) :
    BaseQuickAdapter<UploadItem, BaseViewHolder>(R.layout.layout_upload_item, list) {
    private val mContext = context

    init {
        addChildClickViewIds(R.id.iv_delete_img, R.id.iv_image, R.id.iv_pdf)
    }

    override fun convert(holder: BaseViewHolder, item: UploadItem) {
        val itemType = item.itemType
        holder.setGone(R.id.iv_image, true)
        holder.setGone(R.id.iv_pdf, true)
        holder.setGone(R.id.ll_error_container, true)
        holder.setGone(R.id.fl_add_container, true)
        holder.setGone(R.id.iv_delete_img, true)
        holder.setGone(R.id.ll_progress_container, true)
        when (item.uploadState) {
            UploadItem.State.UPLOADING -> {
                holder.setGone(R.id.ll_progress_container, false)
                holder.setText(R.id.tv_upload_progress, "${item.progress}%")
            }

            UploadItem.State.FAILED -> {
                holder.setGone(R.id.ll_error_container, false)
                holder.setGone(R.id.iv_delete_img, false)
                holder.setText(R.id.tv_upload_error, item.error)
            }

            UploadItem.State.SUCCESS -> {
                holder.setGone(R.id.iv_delete_img, false)
            }
        }
        when (itemType) {
            UploadItem.ADD_ITEM -> {
                holder.setGone(R.id.fl_add_container, false)
            }

            UploadItem.IMAGE_ITEM -> {
                if (item.uploadState == UploadItem.State.SUCCESS) {
                    holder.setGone(R.id.iv_image, false)
                    val radius = mContext.resources.getDimension(
                        dimen.dp_12
                    ).toInt()
                    holder.getView<ImageView>(R.id.iv_image)
                        .loadRoundImage(
                            item.url,
                            radius
                        )
                }
            }

            UploadItem.PDF_ITEM -> {
                holder.setGone(R.id.iv_pdf, false)
            }
        }

    }
}